package scaleService;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Element;

import util.Generator;
import dataObject.DataType;
import dataObject.Operation;
import dataObject.Sensor;
import exception.DataTypeException;
import exception.UserException;

public class DeleteDataType extends HttpServlet
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6288290312743628849L;

	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
		try
		{
			String typeName = req.getParameter("typeName");
			DataType targetDataType=DataType.getDataType(typeName);
			if(targetDataType==null)
				throw new DataTypeException(DataTypeException.DataTypeNotFonund);
			List<Operation> operationList=Operation.getOperationList(typeName);
			for(Operation o:operationList)
			{
				Operation.deleteDO(o);
			}
			List<Sensor> sensorList=Sensor.getSensorByTypeName(typeName);
			for(Sensor e:sensorList)
			{
				e.setTypeName(DataType.notInNetwork);
			}
			DataType.deleteDO(targetDataType);
			resp.setContentType("text/html;charset=gb2312");
			PrintWriter out=resp.getWriter();
			Element rc=Generator.buildActionReturn(Generator.SUCCESS);
			Transformer transformer = TransformerFactory.newInstance().newTransformer();
			DOMSource source = new DOMSource(rc); 
			StreamResult result = new StreamResult(out);
			transformer.transform(source, result);
		} catch (UserException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DataTypeException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			DataType.closePersistenceManager();
		}
	}
}
